<template>
    <div class='checkout_list'>
        <el-row>
            
            <el-col :span="12">
                <el-table
                    :data="glJson2"
                    stripe
                    border
                    show-summary
                    ref="table1"
                    style="width: 100%;margin-left:5px;"
                    @select="select1"
                    @select-all="selectall1"
                    >
                     <el-table-column
                    type="selection"
                    width="30" v-if="isCheck=='Y'||isCheck=='A'">
                    </el-table-column >
                    <el-table-column
                    prop="szi_ex1"
                    :label="glName"
                    width="140">
                    </el-table-column>
                    <el-table-column
                    prop="num"
                    label="数量"
                    width="80">
                    </el-table-column>
                    <el-table-column
                    prop="amt_total"
                    label="金额">
                    </el-table-column>
                     <el-table-column
                    prop="amt_yh"
                    v-if="isYh=='Y'"
                    label="优惠">
                    </el-table-column>
                    <el-table-column
                  
                    prop="amt_sh"
                    v-if="isYh=='Y'"
                    label="实收">
                  
                    </el-table-column>
                </el-table>
            </el-col>
            <el-col :span="12">
                <el-table
                    :data="gsJson2"
                    stripe
                    border
                    show-summary
                    ref="table2"
                    style="width: 100%;"
                    @select="select2"
                    @select-all="selectall2"
                    >
                      <el-table-column
                    type="selection"
                    width="30" v-if="isCheck=='Y'||isCheck=='B'">
                    </el-table-column >
                    <el-table-column
                    prop="szi_ex1"
                    :label="gsName" 
                    width="140">
                    </el-table-column>
                    <el-table-column
                    prop="num"
                    label="数量"
                    width="80">
                    </el-table-column>
                    <el-table-column
                    prop="amt_total"
                    label="金额">
                    </el-table-column>
                    <el-table-column
                    v-if="isYh=='Y'"
                    prop="amt_yh"
                    label="优惠">
                    </el-table-column>
                     <el-table-column
                  
                    prop="amt_sh"
                    v-if="isYh=='Y'"
                    label="实收">
                   
                    </el-table-column>
                </el-table>
            </el-col>
        </el-row>
       
    </div>
</template>
<script>
export default {
    name:'check_out_list',
    props:['data','appid','exkey','oldData'],
    components: {
    },
    data(){
        return{
            glJson:[],
            gsJson:[],
            glJson2:[],
            gsJson2:[],
            gsName:this.exkey.gsName,
            glName:this.exkey.glName,
            jz_id:'',
            sz_id:'',
            yhItem:null,
            yh:"",
            isCheck:this.exkey.isCheck,
            isYh:this.exkey.isYh,
            glJsonTemp:[],
            gsJsonTemp:[],
            sCode:""
        }
    },
    created(){
        if(localStorage.getItem("sCode")){
            this.sCode=localStorage.getItem("sCode")
        }
        console.log("exkey================================");
        console.log(this.exkey)
        let sef=this;
        this.sz_id=this.data.sz.sz_id;
        //this.loadData();
        window.addEventListener('message', function(messageEvent) {
            var data = messageEvent.data;
            let datas=JSON.stringify(data)
            if(datas.jz_id){
                this.jz_id=datas.jz_id;
            }
        // vue.menuTitle = data;
        },false);
        this.glJson=this.data.gl;
        this.gsJson=this.data.gs;
        this.selectAll();


        //this.loadYh();
    },
    methods:{
        
        selectAll(){
             const that=this
             if(this.isCheck=="Y"||this.isCheck=='A'){
                  that.$nextTick(() => {
                    for (let i = 0; i <that.glJson.length; i++) {
                        that.$refs.table1.toggleRowSelection(
                            that.glJson[i],
                            true
                        );
                    }
                    if(this.isCheck=='Y'||this.isCheck=='B'){
                        for (let i = 0; i <that.gsJson.length; i++) {
                            that.$refs.table2.toggleRowSelection(
                                that.gsJson[i],
                                true
                            );
                        }
                    }
                    
                });
                console.log("this.data=============ssss============================");
                console.log(this.data)

                this.data.jzItem=[];
                for(let i=0;i<this.data.gl.length;i++){
                    this.data.jzItem.push(this.data.gl[i]);
                }
                this.glJsonTemp=this.data.gl;
                if(this.isCheck=='Y'||this.isCheck=='B'){
                    for(let i=0;i<this.data.gs.length;i++){
                        this.data.jzItem.push(this.data.gs[i]);
                    }
                    
                    this.gsJsonTemp=this.data.gs;
                }
             }else{
                    console.log(this.data.jzItem,"jzItem===")
                    //if(!this.data.jzItem||this.data.jzItem.length==0){
                         this.data.jzItem=[];
                    
                        for(let i=0;i<this.data.gl.length;i++){
                            this.data.jzItem.push(this.data.gl[i]);
                        }
                    
                        for(let i=0;i<this.data.gs.length;i++){
                            this.data.jzItem.push(this.data.gs[i]);
                        }
                        
                        this.glJsonTemp=this.data.gl;
                        this.gsJsonTemp=this.data.gs;
                   // }
                   
                   
             }
            try{
                 this.oldData.jzItem=JSON.parse(JSON.stringify(this.data.jzItem))
            }catch(e){}
            
            if( this.data.jzItem.length>0)
            this.setJzItem();
           
        },
        initData(){
            
            console.log("CheckOutinitData======================")
            this.selectAll();
        },
        loadYh(){
            this.$post("bygex","/byg/funeral/getFuneralSfItem",{"type2":"优惠"}).then(data=>{
                let item=data.item;
                if(item.length>0){
                    this.yhItem=item[0];
                }
            })
        },
        select1(selection,row){
            //console.log(selection,"selection================================")
            this.glJsonTemp=selection;
            this.setJzItem()
        },
        selectall1(selection){
            //console.log(selection,"selection222================================")
            this.glJsonTemp=selection;
            this.setJzItem()
        },
        select2(selection,row){
            this.gsJsonTemp=selection;
            this.setJzItem()

        },
        selectall2(selection){
            this.gsJsonTemp=selection;
            this.setJzItem()
        },
        setJzItem(){
           // alert(2)
            this.data.jzItem=[];
            let jz_ex6=0;//公司金额
            let jz_ex7=0;//管理金额
            let jz_recamt=0;//应收金额
            let jz_realamt=0;//实收金额
            let jz_ex14=0;//优惠金额
           
            console.log(this.glJsonTemp,"glJsonTemp=========")
            console.log(this.gsJsonTemp,"gsJsonTemp=========")
            for(let i=0;i<this.glJsonTemp.length;i++){
                this.data.jzItem.push(this.glJsonTemp[i])
                /*if(this.glJsonTemp[i].amt_sh)
                    jz_ex7+=Number(this.glJsonTemp[i].amt_sh);
                else{
                    jz_ex7+=Number(this.glJsonTemp[i].amt_total);
                }*/
                
                let shows=this.glJsonTemp[i].shows;
                
                if(shows!='Y'){
                    jz_ex7+=Number(this.glJsonTemp[i].amt_total);
                    if(this.glJsonTemp[i].szi_ex27)
                        jz_ex14+=Number(this.glJsonTemp[i].szi_ex27);
                    console.log("amt_total==="+Number(this.glJsonTemp[i].amt_total))
                }
                


            }
            for(let i=0;i<this.gsJsonTemp.length;i++){
                this.data.jzItem.push(this.gsJsonTemp[i])

               /* if(this.gsJsonTemp[i].amt_sh)
                    jz_ex6+=Number(this.gsJsonTemp[i].amt_sh);
                else{
                    jz_ex6+=Number(this.gsJsonTemp[i].amt_total);
                }*/
                let shows=this.gsJsonTemp[i].shows;
                if(shows!='Y'){
                    jz_ex6+=Number(this.gsJsonTemp[i].amt_total);
                    if(this.gsJsonTemp[i].szi_ex27)
                        jz_ex14+=Number(this.gsJsonTemp[i].szi_ex27);
                }
                
            }
           
            jz_recamt=jz_ex7+jz_ex6
        
            jz_realamt=jz_recamt-jz_ex14;
            
            console.log(this.glJsonTemp,"this.glJsonTemp=====================");
            if(!this.data["jz"][0].jz_id){
                if(this.sCode=="kmxbyg"){
                    this.$set(this.data["jz"][0],"jz_recamt",jz_recamt );
                    this.$set(this.data["jz"][0],"jz_recamt2",Number(jz_realamt)-Number(jz_ex6) );
                    
                    this.$set(this.data["jz"][0],"jz_realamt",Number(jz_realamt)-Number(jz_ex6));
                    this.$set(this.data["jz"][0],"jz_ex6",jz_ex7);
                    this.$set(this.data["jz"][0],"jz_ex7",jz_ex6);
                    this.$set(this.data["jz"][0],"jz_ex14",jz_ex14);
                }else{
                    this.$set(this.data["jz"][0],"jz_recamt",jz_recamt );
                    this.$set(this.data["jz"][0],"jz_recamt2",jz_recamt );
                    
                    this.$set(this.data["jz"][0],"jz_realamt",jz_realamt);
                    this.$set(this.data["jz"][0],"jz_ex6",jz_ex6);
                    this.$set(this.data["jz"][0],"jz_ex7",jz_ex7);
                    this.$set(this.data["jz"][0],"jz_ex14",jz_ex14);
                }
            }
            
           
            
            
        }
    },
    mounted(){
        let el=document.getElementsByClassName("el-table__footer-wrapper");
        for(let i=0;i<el.length;i++){
            let el2=el[i].getElementsByTagName("td");

            for(let j=0;j<el2.length;j++){
                el2[j].style.background="#66b1ff"
                el2[j].style.color="#66b1ff"
            }
        }
    },
    watch:{
        glJson:{
            handler: function (newVal) {
                let glJson_=[];
                for(let i=0;i<this.glJson.length;i++){
                    let shows=this.glJson[i].shows;
                    if(shows!="Y"){
                        glJson_.push(this.glJson[i])
                    }
                }
                this.glJson2=glJson_;
            },      
            deep: true    //深度监听
        },
        gsJson:{
            handler: function (newVal) {
                let gsJson_=[];
                for(let i=0;i<this.gsJson.length;i++){
                    let shows=this.gsJson[i].shows;
                    if(shows!="Y"){
                        gsJson_.push(this.gsJson[i])
                    }
                }
                this.gsJson2=gsJson_;
            },      
            deep: true    //深度监听
        },
        data:{
            handler: function (newVal) {
                console.log("wathc====================");
                console.log(this.data)
                this.glJson=this.data.gl;
                this.gsJson=this.data.gs;
                console.log(this.data.jzItem)
                
               /* if(this.data["jz"][0].jz_ex13&&this.data["jz"][0].jz_ex13!=this.yh){
                    this.yh=this.data["jz"][0].jz_ex13;
                    this.yhItem.szi_ex2="-"+this.yh;
                    this.yhItem.szi_ex5="-"+this.yh;
                    let index=-1;
                    for(let i=0;i<this.data["jzItem"].length;i++){
                        if(this.data["jzItem"][i].szi_ex7=="优惠"){
                            index=i;
                        }
                    }
                    if(index==-1){
                        index=this.data["jzItem"].length;
                    }
                    this.$set(this.data["jzItem"],index,this.yhItem);
                }
                console.log("items============================");
                console.log(this.yhItem)
               
                */
            },      
            deep: true    //深度监听
        }
    }
}
</script>
<style scoped>
.checkout_list{
    height:100%;
    width:100%;
}
.el-table__footer-wrapper tbody td{
    color:red;
    background-color:#66b1ff;
}
</style>